<?php

namespace App\Admin\Forms\Customer;

use App\Enums\OptionType;
use App\Enums\ReturnChannel;
use App\Enums\ReturnStage;
use App\Enums\SalePerformanceType;
use App\Enums\UserStatus;
use App\Models\Customer;
use App\Models\User;
use App\Services\OptionService;
use App\Services\SaleService;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class ReturnPerformanceForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return JsonResponse
     */
    public function handle(array $input): JsonResponse
    {
        app(SaleService::class)->addReturnPerformance($this->payload['id'], $input);
        return $this
            ->response()
            ->success(trans('admin.processed_successfully'))
            ->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        //业绩信息
        $this->text('disease',trans('return-performance.fields.disease'))->maxLength(155);
        $this->number('money', trans('return-performance.fields.money'))->min(1)->max(999999)->required();
        $this->select('stage', trans('return-performance.fields.stage'))->options(ReturnStage::asSelectArray())->required();
        $this->text('delivery', trans('return-performance.fields.delivery'));
        $this->text('drug', trans('return-performance.fields.drug'));
        $this->text('treatment_course', trans('return-performance.fields.treatment_course'));
        $this->number('subsidy', trans('return-performance.fields.subsidy'))->min(1)->max(999999)->required();
        $this->multipleSelect('invitation_user_ids', trans('return-performance.fields.invitation_user_ids'))->options(User::query()->where('status', UserStatus::WORKING)->pluck('name', 'id'));
        $this->multipleSelect('consultant_user_ids', trans('return-performance.fields.consultant_user_ids'))->options(User::query()->where('status', UserStatus::WORKING)->pluck('name', 'id'));
        $this->select('doctor_option_id', trans('return-performance.fields.doctor_option_id'))->options(OptionService::getOption(OptionType::DOCTOR))->required();
        $this->select('channel', trans('return-performance.fields.channel'))->options(ReturnChannel::asSelectArray())->required();
        $this->datetime('channel_date', trans('return-performance.fields.channel_date'));
        $this->datetime('consultation', trans('return-performance.fields.consultation'));
        $this->multipleImage('pay_images', trans('return-performance.fields.pay_images'))->move('images/' . date('Y') . '/' . date('m') . '/' . date('d'))->uniqueName()->autoUpload();
        $this->textarea('remark', trans('return-performance.fields.remark'));
    }

}
